// grunt 生成快捷方式
const path = eval('require')('path');

const handleStartupEvent = function (app) {
    if (process.platform !== 'win32') {
        return false;
    }
    // 安装
    function install() {
        const cp = eval('require')('child_process');
        const updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe');
        const target = path.basename(process.execPath);
        const child = cp.spawn(updateDotExe, ['--createShortcut', `${target} ../gis.asar`], { detached: true });
        child.on('close', (code) => {
            app.quit();
        });
    }
    // 卸载
    function uninstall() {
        const cp = eval('require')('child_process');
        const updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe');
        const target = path.basename(process.execPath);
        const child = cp.spawn(updateDotExe, ['--removeShortcut', target], { detached: true });
        child.on('close', (code) => {
            app.quit();
        });
    }

    const squirrelCommand = process.argv[1];

    switch (squirrelCommand) {
        case '--squirrel-_install':
            install();
            return true;
        case '--squirrel-updated':
            install();
            return true;
        case '--squirrel-uninstall':
            uninstall();
            app.quit();
            return true;
        case '--squirrel-obsolete':
            app.quit();
            return true;
    }
};

exports.handleStartupEvent = handleStartupEvent;
